package com.eastfair.venuebooking.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.request.PageParams;
import com.eastfair.common.api.PaymentPlanFeign;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.pay.dto.*;
import com.eastfair.pay.vo.PaymentPlanVO;
import com.eastfair.venuebooking.service.VenueBookingPaymentPlanService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * 付款计划
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/paymentPlan")
@Api(value = "PaymentPlan", tags = "付款计划")
@PreAuth(replace = "pay:paymentPlan:")
public class PaymentPlanController {

    @Resource
    private PaymentPlanFeign paymentPlanFeign;

    @Resource
    private VenueBookingPaymentPlanService venueBookingPaymentPlanService;

    /**
     * 更新付款计划
     * @param paymentPlanDTOs
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public R saveOrupdate(@RequestBody @Validated List<PaymentPlanDTO> paymentPlanDTOs){
        R res = paymentPlanFeign.saveOrupdate(paymentPlanDTOs);
        if (res.getIsSuccess()){
            return res;
        } else {
            throw new BizException(res.getCode(),res.getErrorMsg()) ;
        }
    }

    @GetMapping("/delete/{planId}")
    public R delete(@PathVariable("planId") Long planId){
        R res = paymentPlanFeign.delete(planId);
        if (res.getIsSuccess()){
            return res;
        } else {
            throw new BizException(res.getCode(),res.getErrorMsg()) ;
        }
    }


    /**
     * 获取合同付款计划
     * @param contractId
     * @return
     */
    @GetMapping("/queryByContractId/{contractId}")
    public R<List<PaymentPlanVO>> queryByContractId(@PathVariable("contractId") Long contractId){
        R res = paymentPlanFeign.queryByContractId(contractId);
        if (res.getIsSuccess()){
            return res;
        } else {
            throw new BizException(res.getCode(),res.getErrorMsg()) ;
        }
    }


    /**
     * 获取合同付款计划
     * @param params
     * @return
     */
    @PostMapping("/queryPage")
    public R<Page<PaymentPlanVO>> queryPage(@RequestBody @Validated PageParams<PaymentPlanPageQuery> params){
        log.info("queryPage - 获取合同付款计划, params={}", params);
        try {
            Page<PaymentPlanVO> page = venueBookingPaymentPlanService.listForContractManage(params);
            return R.successDef(page);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

}
